#include <stdio.h>
#include <stdlib.h>

#include "data.h"
#include "Tools.h"

#define DEBUG(s,x) fprintf(stderr, "%s -- %d\n", s, x)

int resolution(Creep* creep, Data *data)
{

	int i = 0, j = 0, weight = 0, nb_case_etudie = 1, temp = 1, x=0, y=0, sth_changed = FALSE;

	MapCoor* indice_case;
	indice_case = malloc(data->map.dimension.x * data->map.dimension.y * sizeof(Coor));
	indice_case[0].x = (creep->current.x - data->map.first_block.x)/data->map.dimension_case.x;
	indice_case[0].y = (creep->current.y - data->map.first_block.y)/data->map.dimension_case.y;

	for(i=0; i<data->map.dimension.x; i++){
		for(j=0; j<data->map.dimension.y; j++){
			if(data->game.map[i][j].tower.type == EMPTY || data->game.map[i][j].tower.type == OBSTACLE)
				data->game.map[i][j].weight = 0;
			else
				data->game.map[i][j].weight = -1;
		}
	}

	data->game.map[indice_case[0].x][indice_case[0].y].weight = 0;


	do{
		weight ++;
		sth_changed = FALSE;

		for(i=0; i<nb_case_etudie; i++){
			if(data->game.map[indice_case[i].x][indice_case[i].y].weight == weight-1){
				if(indice_case[i].x-1 >= 0){
					if(data->game.map[indice_case[i].x-1][indice_case[i].y].weight == 0){
						data->game.map[indice_case[i].x-1][indice_case[i].y].weight = weight;
						indice_case[temp].x = indice_case[i].x-1;
						indice_case[temp].y = indice_case[i].y;
						temp++;
						sth_changed = TRUE;
					}
				}
				if(indice_case[i].x+1 < data->map.dimension.x){
					if(data->game.map[indice_case[i].x+1][indice_case[i].y].weight == 0){
						data->game.map[indice_case[i].x+1][indice_case[i].y].weight = weight;
						indice_case[temp].x = indice_case[i].x+1;
						indice_case[temp].y = indice_case[i].y;
						temp++;
						sth_changed = TRUE;
					}
				}
				if(indice_case[i].y-1 >= 0){
					if(data->game.map[indice_case[i].x][indice_case[i].y-1].weight == 0){
						data->game.map[indice_case[i].x][indice_case[i].y-1].weight = weight;
						indice_case[temp].x = indice_case[i].x;
						indice_case[temp].y = indice_case[i].y-1;
						temp++;
						sth_changed = TRUE;
					}
				}
				if(indice_case[i].y+1 < data->map.dimension.y){
					if(data->game.map[indice_case[i].x][indice_case[i].y+1].weight == 0){
						data->game.map[indice_case[i].x][indice_case[i].y+1].weight = weight;
						indice_case[temp].x = indice_case[i].x;
						indice_case[temp].y = indice_case[i].y+1;
						temp++;
						sth_changed = TRUE;
					}
				}
			}
		}
		nb_case_etudie = temp;

		if(!sth_changed){
			return FALSE;
		}
	} while(data->game.map[data->map.final_block.x][data->map.final_block.y].weight == 0);

	x = data->map.final_block.x;
	y = data->map.final_block.y;
	while(weight > 1){
		if(x-1 >= 0){
			if(data->game.map[x-1][y].weight == weight - 1){
				x = x - 1;
				weight --;
			}
		}	
		if(x+1 < data->map.dimension.x){
			if(data->game.map[x+1][y].weight == weight - 1){
				x = x + 1;
				weight --;
			}
		}
		if(y-1 >= 0){
			if(data->game.map[x][y-1].weight == weight - 1){
				y = y - 1;
				weight --;
			}
		}
		if(y+1 < data->map.dimension.y){
			if(data->game.map[x][y+1].weight == weight - 1){
				y = y + 1;
				weight --;
			}
		}
	}

	creep->goal.x = x;
	creep->goal.y = y;

	return TRUE;
}

/////////// PRINT /////////////
/*
	for(i=0; i<data->map.dimension.y;i++){
		for(j=0; j<data->map.dimension.x; j++){
			fprintf(stderr, "%d ", data->game.map[j][i].weight);
		}
		fprintf(stderr, "\n");
	}
	fprintf(stderr, "\n");
*/